package com.inspur.ourhis_afterend.Controller;


import com.inspur.ourhis_afterend.Entity.Member;
import com.inspur.ourhis_afterend.Entity.User;
import com.inspur.ourhis_afterend.Service.MemberService;
import com.inspur.ourhis_afterend.Tools.PageAction;
import com.inspur.ourhis_afterend.Tools.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    MemberService memberService;

    // 用户添加
    @ApiOperation("添加或者更新会员")
    @PostMapping("/add")
    @ApiParam(value = "传入一个会员对象")
    public Result add(@RequestBody Member member) {
        System.out.println(member);
        try {
            memberService.add(member);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    // 用户删除
    @ApiOperation("会员删除")
    @PostMapping("/del")
    @ApiParam(value = "传入一个用户id")
    public Result del(@Param("memberid") Integer memberid) {
        try {
            memberService.del(memberid);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }


    // 查询全部用户
    @ApiOperation("查询全部会员")
    @PostMapping("/findAll")
    @ApiParam(value = "传入分页对象")
    public Result findAll(@RequestBody PageAction pageAction) {

        try {
            return Result.success(memberService.findAll(pageAction));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    /**
     * 查询单个用户  用于表单回显
     * @param memberid
     * @return
     */
    @PostMapping("/findOne")
    @ApiOperation("查询单个用户")
    @ApiParam(value = "传入一个用户id")
    public Result findOne(@Param("memberid") Integer memberid) {
        System.out.println(memberid);
        try {
            return Result.success(memberService.findOne(memberid));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    /**
     * 查询
     * @param realName
     * @return
     */
    @PostMapping("/search")
    @ApiOperation("通过用户名查询会员")
    @ApiParam(value = "传入查询条件")
    public Result search(@RequestParam("memberid") String memberid,
                         @RequestParam("realname") String realname,
                         @RequestParam("credit") String credit,
                         @RequestParam("tel") String tel) {
        try {
            return Result.success(memberService.search(memberid, realname, credit, tel));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }


    @PostMapping("/increase")
    @ApiOperation("通过用户名查询单个用户")
    @ApiParam(value = "传入一个用户姓名")
    public Result increase(@RequestParam("memberid") String memberid,
                           @RequestParam("money") double money) {
        try {
            return Result.success(memberService.increase(memberid, money));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

   /* *//**
     * 通过用户名查询单个用户
     * @param realName
     * @return
     *//*
    @PostMapping("/findByName")
    @ApiOperation("通过用户名查询单个用户")
    @ApiParam(value = "传入一个用户姓名")
    public Result findByName(@Param("realName") String realName) {
        System.out.println(realName);
        try {
            return Result.success(userService.findByName(realName));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }


    *//**
     * 通过用户类型查询多个用户
     * @param role
     * @return
     *//*
    @ApiOperation("通过用户类型查询多个用户")
    @ApiParam(value = "传入一个用户类型")
    @PostMapping("/findByRole")
    public Result findByRole(@Param("role") String role) {
        System.out.println(role);
        try {
            return Result.success(userService.findByRole(role));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }*/
}
